const md5 = require('md5');
const rp = require('request-promise');
const crypto = require('crypto');
const moment = require('moment/moment.js');
const { think } = require('thinkjs');

module.exports = class extends think.Service {
  /**
   * 生成签名字符串
   * @param {Object} data 参数对象
   * @param {String} nonce 随机字符串
   * @returns {String} 签名字符串
   */
  getSign(data, nonce) {
    // 获取关键字列表并排序
    const keys = Object.keys(data).sort();
    let signStr = '';

    // 拼接所有value值
    for (const key of keys) {
      signStr += data[key];
    }

    // 加上随机字符串
    signStr += nonce;

    // 计算MD5值
    return this.getMD5(signStr);
  }

  /**
   * MD5加密
   * @param {String} text 要加密的文本
   * @returns {String} 32位小写MD5字符串
   */
  getMD5(text) {
    const hash = crypto.createHash('md5');
    hash.update(text, 'utf8');
    return hash.digest('hex');
  }

  async chaobiao(request_content) {
    try {
      const timestamp2 = moment()
        .unix()
        .toString();
      const body = {
        timestamp: timestamp2,
        notify_url: 'http://api.qiyikeji.cn/admin/interval/getChaobiao',
        auth_code: '96cf3b0407817c6aec7002a0f7e67464',
        request_content: JSON.stringify(request_content),
      };

      console.log('body:', body);

      const nonce = 'kJNdHdFdTsn4XuLfgeJO9Vk';
      const sign = this.getSign(body, nonce);

      console.log('生成的签名:', sign);

      const options = {
        method: 'POST',
        url: 'http://api2.tqdianbiao.com/Api_v2/ele_read',
        headers: {
          // 'x-access-token': videoToken,
        },
        json: true,
        body: {
          timestamp: timestamp2,
          auth_code: '96cf3b0407817c6aec7002a0f7e67464',
          request_content: JSON.stringify(request_content),
          notify_url: 'http://api.qiyikeji.cn/admin/interval/getChaobiao',
          sign: sign,
        },
      };

      console.log('请求参数:', options);

      const sessionData = await rp(options);
      console.log('响应数据:', sessionData);
      return sessionData;
    } catch (e) {
      console.error('请求异常:', e);
      return null;
    }
  }

  async shuibiao(request_content) {
    try {
      const timestamp2 = moment()
        .unix()
        .toString();
      const body = {
        timestamp: timestamp2,
        notify_url: 'http://api.qiyikeji.cn/admin/interval/getShuibiao',
        auth_code: '96cf3b0407817c6aec7002a0f7e67464',
        request_content: JSON.stringify(request_content),
      };

      const nonce = 'kJNdHdFdTsn4XuLfgeJO9Vk';
      const sign = this.getSign(body, nonce);

      console.log('生成的签名:', sign);

      const options = {
        method: 'POST',
        url: 'http://api2.tqdianbiao.com/Api_v2/water_read',
        headers: {
          // 'x-access-token': videoToken,
        },
        json: true,
        body: {
          timestamp: timestamp2,
          auth_code: '96cf3b0407817c6aec7002a0f7e67464',
          request_content: JSON.stringify(request_content),
          notify_url: 'http://api.qiyikeji.cn/admin/interval/getShuibiao',
          sign: sign,
        },
      };

      console.log('请求参数:', options);

      const sessionData = await rp(options);
      console.log('响应数据:', sessionData);
      return sessionData;
    } catch (e) {
      console.error('请求异常:', e);
      return null;
    }
  }
};
